package com.hrs.core.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hrs.common.anno.NoNeedLogin;
import com.hrs.common.anno.NoValidPrivilege;
import com.hrs.common.anno.NotOpen;
import com.hrs.common.anno.RequestLimit;
import com.hrs.common.constant.SwaggerConst;
import com.hrs.common.constant.VerificationConst;
import com.hrs.common.result.Result;
import com.hrs.core.service.UserService;
import com.hrs.enity.db.User;
import com.hrs.enity.dto.UserViewDto;
import com.hrs.enity.ro.LoginFormRo;
import com.hrs.enity.ro.LoginResponseRo;
import com.hrs.enity.ro.UserRegisterRo;
import com.hrs.enity.vo.KaptchaVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author 摆渡人
 * @description
 * @date 2022/12/6 10:09
 */
@Slf4j
@Api(tags = {SwaggerConst.Admin.MANAGER_USER_LOGIN})
@Validated
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/user/login")
    @ApiOperation(value = "登录", notes = "@author 摆渡人")
    @NoNeedLogin
    public Result<LoginResponseRo> login(@Validated @RequestBody LoginFormRo loginForm, HttpServletRequest request) {
        return userService.login(loginForm, request);
    }

    @GetMapping("/user/vCode")
    @ApiOperation(value = "获取图片验证码", notes = "@author 摆渡人")
    @NoNeedLogin
    @RequestLimit(code = "user:phoneCode:",seconds = 10)
    public Result<KaptchaVO> verificationCode() {
        return userService.sendImgCode();
    }

    @GetMapping("/user/logOut")
    @ApiOperation(value = "退出登陆", notes = "@author 摆渡人")
    @NoValidPrivilege
    public Result<String> logOut() {
        return userService.loginOut();
    }

    @GetMapping("/user/pCode/{phone}")
    @ApiOperation(value = "获取手机验证码",notes = "@author 摆渡人")
    @NoNeedLogin
    @RequestLimit(code = "user:phoneCode:",seconds = 10)
    public Result<String> phoneCode(@PathVariable("phone") String phone){
        if(StringUtils.isBlank(phone)){
            return Result.error("请输入手机号");
        }

        if(!phone.matches(VerificationConst.PHONE_REGEXP)){
            return Result.error("手机格式错误");
        }

        return userService.sendPhoneCode(phone);
    }

    @PostMapping("/user/register")
    @ApiOperation(value = "用户注册")
    @NoNeedLogin
    public Result<String> register(@Validated @RequestBody UserRegisterRo userRegisterRo) {
        return userService.register(userRegisterRo);
    }

    @GetMapping("/user/list/{roleId}")
    @ApiOperation(value = "用户下拉框")
    @NoValidPrivilege
    public Result<List<UserViewDto>> listUser(@PathVariable Integer roleId){
        return userService.listUser(roleId);
    }
}
